package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.GoodsCategoryMapper;
import com.mysterymall.Mapper.GoodsTypeMapper;
import com.mysterymall.Model.Entity.GoodsCategory;
import com.mysterymall.Model.Entity.GoodsType;
import com.mysterymall.Model.Vo.GoodsCategoryDto;
import com.mysterymall.Model.Vo.GoodsTypeVo;
import com.mysterymall.Service.GoodsCategoryService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/11/29  13:28
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    private final GoodsCategoryMapper goodsCategoryMapper;

    private final GoodsTypeMapper goodsTypeMapper;

    /**
     * 构造器注入
     *
     * @param goodsCategoryMapper 商品类别映射器
     */
    public GoodsCategoryServiceImpl(GoodsCategoryMapper goodsCategoryMapper, GoodsTypeMapper goodsTypeMapper) {
        this.goodsCategoryMapper = goodsCategoryMapper;
        this.goodsTypeMapper = goodsTypeMapper;
    }


    @Override
    public Result addGoodsCategory(String name) {

        if (!StpUtil.hasPermission("goodsCategory:add:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }
        LambdaQueryWrapper<GoodsCategory> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(GoodsCategory::getName, name);

        GoodsCategory goodsCategory = goodsCategoryMapper.selectOne(queryWrapper);

        if (goodsCategory != null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.alikeError");
        }

        goodsCategory = new GoodsCategory();

        goodsCategory.setName(name);

        goodsCategoryMapper.insert(goodsCategory);

        return new Result(HttpStatus.SUCCESS, "message.addSuccess");

    }

    @Override
    public Result getGoodsCategory() {

        if (!StpUtil.hasPermission("goodsCategory:select:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        LambdaQueryWrapper<GoodsCategory> queryWrapper = new LambdaQueryWrapper<>();

        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectList(queryWrapper);

        return new Result(HttpStatus.SUCCESS, "message.querySuccess", goodsCategoryList);
    }

    @Override
    public Result updateGoodsCategory(GoodsCategory goodsCategory) {
        return null;
    }

    @Override
    public Result deleteGoodsCategory(String id) {

        if (!StpUtil.hasPermission("goodsCategory:delete:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }
        LambdaQueryWrapper<GoodsType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsType::getCategoryId, id);
        List<GoodsType> goodsTypeList = goodsTypeMapper.selectList(queryWrapper);
        if (goodsTypeList.size() > 0) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.deleteError-该类别下有商品类别不允许删除");
        }
        goodsCategoryMapper.deleteById(id);

        return new Result(HttpStatus.SUCCESS, "message.deleteSuccess");
    }

    @Override
    public Result getShowGoodsCategory(HttpServletRequest request) {

        LambdaQueryWrapper<GoodsCategory> queryWrapper = new LambdaQueryWrapper<>();
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectList(queryWrapper);

        List<GoodsCategoryDto> goodsCategoryDtoList = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            LambdaQueryWrapper<GoodsType> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(GoodsType::getCategoryId, goodsCategory.getId());
            List<GoodsType> goodsTypeList = goodsTypeMapper.selectList(queryWrapper1);


            GoodsCategoryDto goodsCategoryDto = new GoodsCategoryDto();

            goodsCategoryDto.setGoodsCategoryId(goodsCategory.getId());
            int picIndex = -1;
            switch ((String) request.getAttribute("Lang")) {
                case "zh":
                    picIndex = 0;
                    goodsCategoryDto.setName(goodsCategory.getName().split("_")[0]);
                    break;
                case "ms":
                    picIndex = 1;
                    goodsCategoryDto.setName(goodsCategory.getName().split("_")[1]);
                    break;
                default:
                    picIndex = 2;
                    goodsCategoryDto.setName(goodsCategory.getName().split("_")[2]);
                    break;
            }

            List<GoodsTypeVo> goodsTypeDtoList = new ArrayList<>();
            for (GoodsType goodsType : goodsTypeList) {
                GoodsTypeVo goodsTypeDto = new GoodsTypeVo();
                goodsTypeDto.setId(goodsType.getId());
                goodsTypeDto.setGoodsTypeZh(goodsType.getTypeName().split("_")[picIndex]);
                goodsTypeDto.setImage("/images/" + goodsType.getImage());
                goodsTypeDto.setCreateTime(goodsType.getCreateTime());
                goodsTypeDtoList.add(goodsTypeDto);
            }
            goodsCategoryDto.setGoodsTypeIdList(goodsTypeDtoList);
            goodsCategoryDtoList.add(goodsCategoryDto);
        }
        return new Result(HttpStatus.SUCCESS, "message.querySuccess", goodsCategoryDtoList);
    }
}
